# +----------------------------------------------------------------------
# | ChatWork智能聊天办公系统
# +----------------------------------------------------------------------
# | 软件声明: 本系统并非自由软件,未经授权任何形式的商业使用均属非法。
# | 版权保护: 任何企业和个人不允许对程序代码以任何形式任何目的复制/分发。
# | 授权要求: 如有商业使用需求,请务必先与版权所有者取得联系并获得正式授权。
# +----------------------------------------------------------------------
# | Author: ChatWork Team <2474369941@qq.com>
# +----------------------------------------------------------------------
from exception import AppException
from common.utils.config import ConfigUtil
from apps.admin.schemas.apply.draws import setting_schema as schema


class DrawsSettingService:

    @classmethod
    async def mj_detail(cls) -> schema.MjSetting:
        """
        MJ配置详情

        Returns:
            schema.MjSetting

        Author:
            zero
        """
        config = await ConfigUtil.get("draws", "mj") or {}
        mode = config.get("mode") or {}
        return schema.MjSetting(
            name=str(config.get("name", "")),
            status=int(config.get("status", "0")),
            channel=config.get("channel", "mj_zsy"),
            mode=schema.MjModeOptions(
                fast=schema.MjModelParams(
                    status=bool(mode.get("fast", {}).get("status", False)),
                    points=int(mode.get("fast", {}).get("points", "0"))
                ),
                relax=schema.MjModelParams(
                    status=bool(mode.get("relax", {}).get("status", False)),
                    points=int(mode.get("relax", {}).get("points", "0"))
                ),
                turbo=schema.MjModelParams(
                    status=bool(mode.get("turbo", {}).get("status", False)),
                    points=int(mode.get("turbo", {}).get("points", "0"))
                )
            )
        )

    @classmethod
    async def mj_save(cls, post: schema.MjSetting) -> None:
        """
        MJ配置保存

        Args:
            post (schema.MjSetting)

        Author:
            zero
        """
        mode = False
        for item in post.mode:
            if item[1].status:
                mode = True
                break

        if not mode:
            raise AppException("请至少勾选1个生成模式")

        vo = schema.MjSetting(
            name=post.name,
            status=post.status,
            channel=post.channel,
            mode=schema.MjModeOptions(
                fast=schema.MjModelParams(
                    status=post.mode.fast.status,
                    points=post.mode.fast.points
                ),
                relax=schema.MjModelParams(
                    status=post.mode.relax.status,
                    points=post.mode.relax.points
                ),
                turbo=schema.MjModelParams(
                    status=post.mode.turbo.status,
                    points=post.mode.turbo.points
                )
            )
        )

        await ConfigUtil.set("draws", "mj", vo.model_dump_json())

    @classmethod
    async def sd_detail(cls) -> schema.SdSetting:
        """
        SD配置详情

        Returns:
            schema.SdSetting

        Author:
            zero
        """
        config = await ConfigUtil.get("draws", "sd") or {}
        return schema.SdSetting(
            status=int(config.get("status", "0")),
            points=int(config.get("points", "0")),
            name=str(config.get("name", "")),
            agency_api=str(config.get("agency_api", ""))
        )

    @classmethod
    async def sd_save(cls, post: schema.SdSetting) -> None:
        """
        SD配置保存

        Args:
            post (schema.SdSetting)

        Author:
            zero
        """
        await ConfigUtil.set("draws", "sd", schema.SdSetting(
            status=post.status,
            points=post.points,
            name=post.name,
            agency_api=post.agency_api
        ).model_dump_json())

    @classmethod
    async def dalle_detail(cls) -> schema.DalleSetting:
        """
        SD配置详情

        Returns:
            schema.SdSetting

        Author:
            zero
        """
        config = await ConfigUtil.get("draws", "dalle") or {}
        return schema.DalleSetting(
            status=int(config.get("status", "0")),
            points=int(config.get("points", "0")),
            name=str(config.get("name", "")),
            agency_api=str(config.get("agency_api", ""))
        )

    @classmethod
    async def dalle_save(cls, post: schema.DalleSetting) -> None:
        """
        DALLE配置保存

        Args:
            post (schema.DalleSetting)

        Author:
            zero
        """
        await ConfigUtil.set("draws", "dalle", schema.DalleSetting(
            status=post.status,
            points=post.points,
            name=post.name,
            agency_api=post.agency_api
        ).model_dump_json())

    @classmethod
    async def doubao_detail(cls) -> schema.DoubaoSetting:
        """
        豆包配置详情

        Returns:
            schema.DoubaoSetting

        Author:
            zero
        """
        config = await ConfigUtil.get("draws", "doubao") or {}
        return schema.DoubaoSetting(
            status=int(config.get("status", "0")),
            points=int(config.get("points", "0")),
            name=str(config.get("name", ""))
        )

    @classmethod
    async def doubao_save(cls, post: schema.DoubaoSetting) -> None:
        """
        豆包配置保存

        Args:
            post (schema.DoubaoSetting)

        Author:
            zero
        """
        await ConfigUtil.set("draws", "doubao", schema.DoubaoSetting(
            status=post.status,
            points=post.points,
            name=post.name
        ).model_dump_json())
